<?php

	class Backend_ManageController extends Zend_Controller_Action {
		
		public function init() {
			
			$am = new Model_Authentication();
			$user = $am->getlogguedInformations();
			
			$this->view->action_name = $this->getRequest()->getActionName();
			$this->view->controller_name = $this->getRequest()->getControllername();
		
			// Logged
			if($am->isLoggued()){
				
				// if admin
				$admin = false;
				foreach($user['groups'] as $g) {
					if(strtolower($g) == 'admin') {
						$admin = true;
					}
				}
				
				if(!$admin) {
					$redirector = $this->_helper->getHelper('Redirector');
					$redirector->gotoRoute(array('controller' => 'index', 'action' => 'index'));
				}
				
				$this->view->is_logged = true;
				$this->view->user = $user;
				
			// Not logged
			} else {
				$redirector = $this->_helper->getHelper('Redirector');
				$redirector->gotoRoute(array('module' => 'frontend', 'controller' => 'index', 'action' => 'index'));
			}
			
			$this->view->headLink()
					->appendStylesheet($this->_request->getBaseUrl().'/styles/commun.css')
					->appendStylesheet($this->_request->getBaseUrl().'/styles/template.css')
					->appendStylesheet($this->_request->getBaseUrl().'/styles/backend/manage.css');
			
			// Ajax's call
		    $this->_helper->AjaxContext()
		         ->addActionContext('prc-signin', 'json')
		         ->initContext();
		    
		    $this->view->baseUrl = $this->_request->getBaseUrl();
			
			// change layout
            $layout = Zend_Layout::getMvcInstance();
			$layout->setLayout('layout_admin');
		}
		
		public function usersAction() {
		
			$this->view->headTitle('Hybrid Legacy - Administration - Gérer les utilisateurs');
			
			$this->view->jQuery()
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.form.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.rules_custom.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.messages_fr.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/backend/manage-users.js'));
			
			// get all users
			$uM = new Model_User();
			$filter = array();
			$order = 'firstname';
			
			$users_temp = $uM->get($filter, $order);
			
			// only users
			$users = array();
			foreach($users_temp as $u) {
				foreach($u['groups'] as $g) {
					if(strtolower($g) == 'member') {
						$users[] = $u;
					}
				}
			}
			
			$this->view->users = $users;
			
		}
		
		public function articlesAction() {
		
			$this->view->headTitle('Hybrid Legacy - Administration - Gérer les articles');
			
			$this->view->jQuery()
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.form.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.rules_custom.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.messages_fr.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/backend/manage-articles.js'));
			
			// get all users
			$uM = new Model_Article();
			$filter = array('visible' => 1);
			$order = 'id DESC';
			
			$articles = $uM->get($filter, $order);
			
			$this->view->articles = $articles;
			
		}
		
		public function agendaAction() {
			
			$this->view->headTitle('Hybrid Legacy - Administration - Gérer l\'agenda');
			
			$this->view->jQuery()
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.form.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.rules_custom.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.messages_fr.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/backend/manage-agenda.js'));
			
			// get all date
			$uM = new Model_Agenda();
			$filter = array('visible' => 1);
			$order = 'id DESC';
			
			$events = $uM->get($filter, $order);
			
			// change date format
			foreach($events as &$e) {
				$date = new Zend_Date($e['start']);
				$e['start'] = $date->get(Zend_Date::DAY).'/'.$date->get(Zend_Date::MONTH).'/'.$date->get(Zend_Date::YEAR);
			}
			
			$this->view->events = $events;
			
		}
	
		/**
		 * Users' management formular
		 *
		 * @return html
		 *
		 */
		public function indexAction() {
			
			$this->view->headTitle('APEL Workflow - Administration');
			
			$this->view->jQuery()
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.form.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.rules_custom.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.messages_fr.js'));
					
			// get all structure for which user is validator
			$ugM = new Model_UsersGroups();
			$filters = array('id_user' => $this->view->user['id'],
							'name' => array('validator', 'validator_icf'),
							);
			$groups = $ugM->get($filters);
			
			if(empty($groups)) {
				$this->view->waiting_users = array();
				return;
			}
			
			$structures = array();
			$selected_structures = array();
			$icf = false;
			foreach($groups AS $g) {
				//if(!in_array($g['id_structure'], $selected_structures)) {
					$structures[] = $g['id_structure'];
					if(preg_match('/icf/i', $g['name'])) {
						$icf = true;
					}
				//}
				$selected_structures[] = $g['id_structure'];
			}
			// get all related users
			$waiting_users = $ugM->getWaitingUsers($this->view->user['id'], $structures, $icf);
			
			// pre($waiting_users, 'dump');
			
			$this->view->waiting_users = $waiting_users;
			
			/*
			$users = array();
			foreach($groups AS $g) {
				$users[] = $this->_annuaire->get('person', $g['id_user']);
			}
			pre($users);
			*/
		}
		
		/*
		 * Get validator's logs
		 *
		 */
		public function logsAction() {
			
			$this->view->jQuery()
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.form.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.rules_custom.js'))
					->addJavascriptFile($this->view->baseUrl('/javascript/jquery/jquery.validation.messages_fr.js'));
					
			// get all structure for which user is validator
			$ugM = new Model_UsersGroups();
			$filters = array('id_user' => $this->view->user['id'],
							'name' => array('validator', 'validator_icf'),
							);
			$groups = $ugM->get($filters);
			
			if(empty($groups)) {
				$this->view->logged_users = array();
				return;
			}
			
			$structures = array();
			$selected_structures = array();
			$icf = false;
			foreach($groups AS $g) {
				if(!in_array($g['id_structure'], $selected_structures)) {
					$structures[] = $g['id_structure'];
					
					if(preg_match('/icf/i', $g['name'])) {
						$icf = true;
					}
				}
				$selected_structures[] = $g['id_structure'];
			}
			
			// get all logged users
			$logged_users = $ugM->getLoggedUsers($this->view->user['id'], $structures, $icf);
			
			$this->view->logged_users = $logged_users;
		}
		
		/**
		 *
		 * User's registration's script
		 *
		 * @params array
		 *
		 */
		public function registration($registrations) {
			
			$accepted = array();
			$rejected = array();
			foreach($registrations AS $r) {
				$temp = explode('_', $r);
				if($temp[0] == 'accepted') {
					$accepted[] = array('id_user' => $temp[1],
										'id_structure' => $temp[2],
										'icf' => ($temp[3] == '1') ? true : false
										);
				
				} else if($temp[0] == 'rejected') {
					$rejected[] = array('id_user' => $temp[1],
										'id_structure' => $temp[2],
										'icf' => ($temp[3] == '1') ? true : false
										);
				}
			
			}
			
			//return $accepted;
			
			$ugM = new Model_UsersGroups();
			$result = 'Aucune modification effectuée';	// If no change
				
			if(!empty($accepted)) {
				$result = $ugM->accept($accepted, $this->view->user['id']);
				if($result == 'OK') {
					// Send confirmation mail
					$eM = new Model_EmailsManager();
					$object = 'registration_validate';
					foreach($accepted AS $a) {
						$user_temp = $this->_richcore->getuserbyid($a['id_user']);
						var_dump($user_temp);
						$to = $user_temp[0]['login'];
						$eM->send($object, $to);
					}
				}
			}
			
			if(!empty($rejected)) {
				$result = $ugM->reject($rejected, $this->view->user['id']);
				if($result == 'OK') {
					// Send confirmation mail
					$eM = new Model_EmailsManager();
					$object = 'registration_reject';
					foreach($rejected AS $a) {
						$user_temp = $this->_richcore->getuserbyid($a['id_user']);
						$to = $user_temp[0]['login'];
						$eM->send($object, $to);
					}
				}
			}
			
			return $result;
		}
		
		/**
		 *
		 * User's renewall's script
		 *
		 * @params array
		 *
		 */
		public function renewall($renewall) {
			
			$accepted = array();
			$rejected = array();
			foreach($renewall AS $r) {
				$temp = explode('_', $r);
				if($temp[0] == 'accepted') {
					$accepted[] = array('id_user' => $temp[1],
										'id_structure' => $temp[2],
										'icf' => ($temp[3] == '1') ? true : false
										);
				
				} else if($temp[0] == 'rejected') {
					$rejected[] = array('id_user' => $temp[1],
										'id_structure' => $temp[2],
										'icf' => ($temp[3] == '1') ? true : false
										);
				}
			
			}
			
			//return $renewall;
			
			$ugM = new Model_UsersGroups();
			$result = 'Aucune modification effectuée';	// If no change
				
			if(!empty($accepted)) {
				$result = $ugM->acceptRenewall($accepted, $this->view->user['id']);
				if($result == 'OK') {
					// Send confirmation mail
					$eM = new Model_EmailsManager();
					$object = 'renewall_validate';
					foreach($accepted AS $a) {
						$user_temp = $this->_richcore->getuserbyid($a['id_user']);
						$to = $user_temp[0]['login'];
						$eM->send($object, $to);
					}
				}
			}
			
			if(!empty($rejected)) {
				$result = $ugM->rejectRenewall($rejected, $this->view->user['id']);
				if($result == 'OK') {
					// Send confirmation mail
					$eM = new Model_EmailsManager();
					$object = 'renewall_reject';
					foreach($rejected AS $a) {
						$user_temp = $this->_richcore->getuserbyid($a['id_user']);
						$to = $user_temp[0]['login'];
						$eM->send($object, $to);
					}
				}
			}
			
			return $result;
		}
	
		/**
		 * Users' management script
		 *
		 * @return String
		 *
		 */
		public function manageapplyAction() {
			
			//$type = $this->_request->getParam('management');
			
			//exit($type);
			$datas = $this->_request->getPost();
			
			$registrations = array();
			$renewall = array();
			foreach($datas AS $key => $d) {
				$temp = explode('_', $key);
				
				if($temp[0] == 'status') {
					if($temp[1] == 'waiting') {
						$registrations[] = $d;
					
					} else if($temp[1] == 'renewall') {
						$renewall[] = $d;
					
					}
				}
			}
			
			// Registrations
			if(!empty($registrations)) {
				$return = array('return' => $this->registration($registrations));
			
			// Renewall
			} else if(!empty($renewall)) {
				$return = array('return' => $this->renewall($renewall));
			
			} else {
				$return = array('return' => 'Aucune modification effectuée');
			}
			
			exit(Zend_Json::encode($return));
		}
		
		
		/**
		 *
		 * Manage logs
		 *
		 * @param array $users
		 *
		 * @return string
		 *
		 */
		 public function logsapplyAction() {
		 	$accepted_temp = $this->_request->getParam('logs_accept');
		 	$rejected_temp = $this->_request->getParam('logs_reject');
		 	
		 	$accepted = array();
		 	$rejected = array();
		 	
		 	if(!is_null($accepted_temp)) {
		 	foreach($accepted_temp AS $a) {
		 		$temp = explode('_', $a);
		 		$accepted[] = array('id_user' => $temp[1],
									'id_structure' => $temp[2],
									'icf' => ($temp[3] == '1') ? true : false
									);
				
			}
			}
			
			if(!is_null($rejected_temp)) {
			foreach($rejected_temp As $r) {
				$temp = explode('_', $r);
				$rejected[] = array('id_user' => $temp[1],
									'id_structure' => $temp[2],
									'icf' => ($temp[3] == '1') ? true : false
									);
			}
			}
			
		 	$ugM = new Model_UsersGroups();
			
			$result = 'Aucune modification effectuée';	// If no change
			
		 	if(!empty($accepted)) {
		 		$result = $ugM->accept($accepted, $this->view->user['id']);
		 	}
		 	
		 	if(!empty($rejected)) {
		 		$result = $ugM->reject($rejected, $this->view->user['id']);
		 	}
		 	
		 	$return = array('return' => $result);
		 	
		 	exit(Zend_Json::encode($return));
		 }
	
	}
	
?>
